//
//  PublicityTableViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/10/22.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "PublicityTableViewModel.h"

#import "CommonPublicityItemViewModel.h"

#import "ArticlesListData.h"

@interface PublicityTableViewModel()

@property (nonatomic, readwrite, strong) RACCommand* requestDataCmd;

@property (nonatomic, readwrite, strong) RACCommand* selectCmd;

@end

@implementation PublicityTableViewModel

- (void)initialize
{
    [super initialize];
    
    @weakify(self);
    
    /// viewDidLoad后加载数据
    self.shouldRequestRemoteDataOnViewDidLoad = YES;
    /// 允许下拉刷新
    self.shouldPullDownToRefresh = NO;
    /// 允许上拉加载
    self.shouldPullUpToLoadMore = NO;
    
    /// 请求数据
    self.requestDataCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id inputInfo) {
        
        @strongify(self);
        
        NSInteger loadIndex = [inputInfo[@"LoadIndex"] integerValue];
        
        return [self requestListDataSignalWithPageIndex:loadIndex pageSize:List_PageSize];
    }];
    
    /// 选中
    self.selectCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id x) {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0), dispatch_get_main_queue(), ^
            {
//                CommonPublicityItemViewModel* itemViewModel = self.dataSource[[indexRow integerValue]];
                
                [subscriber sendNext:x];
                [subscriber sendCompleted];
            });
            return nil;
        }];
    }];
    /// 允许并行执行
    self.selectCmd.allowsConcurrentExecution = NO;
}

- (RACSignal *)requestListDataSignalWithPageIndex:(int)pageIndex pageSize:(int)pageSize
{
    @weakify(self)
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self)
        
        NSDictionary* postData = interface_param_getcatelist(self.cateID, self.areaID, @(pageIndex+1), @(pageSize));
        
        @weakify(self);
        return [[AFNetWorkUtils racPOSTWthURL:url_interface_getcatelist params:postData] subscribeNext:^(id x)
                {
                    @strongify(self)
                    
                    /// 转化数据
                    @weakify(self);
                    
                    ArticlesListData* articlesListData = [ArticlesListData modelWithJSON:x];
                    
                    NSArray *viewModels = [articlesListData.datas.rac_sequence map:^CommonPublicityItemViewModel *(ArticleListData* data)
                                           {
                                               @strongify(self);
                                               
                                               CommonPublicityItemViewModel *itemViewModel = [[CommonPublicityItemViewModel alloc] initWithData:data cellHeight:self.cellHeight];
                                               
                                               return itemViewModel;
                                           }].array;
                    
                    if (self.shouldPullUpToLoadMore)
                    {
                        if (0 == pageIndex)
                        {
                            self.dataSource = viewModels;
                        }
                        else
                        {
                            if (self.dataSource == nil)
                            {
                                self.dataSource = viewModels;
                            }
                            else
                            {
                                self.dataSource = [self.dataSource arrayByAddingObjectsFromArray:viewModels];
                            }
                        }
                    }
                    else
                    {
                        self.dataSource = viewModels;
                    }
                    
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                }   error:^(NSError *error) {
                    //错误处理
                    
                    [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:error.userInfo[customErrorInfoKey]}]];
                    [subscriber sendCompleted];
                }
                ];
        
        return nil;
    }];
}

@end
